package com.hdyg.sixg.activity.home;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.CurrentMoneyAdapter;
import com.hdyg.sixg.adapter.PayMethodAdapter;
import com.hdyg.sixg.entry.BuyMiniBean;
import com.hdyg.sixg.entry.CurrentMoneyBean;
import com.hdyg.sixg.mvp.Contrant.CCurrentMoney;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PCurrentMoney;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.PopWindowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.List;
import butterknife.BindView;

/**
 * @author CZB
 * @describe 流通钱包
 * @time 2019/4/8 11:13
 */

public class CurrentMoneyActivity extends BaseActivity implements CCurrentMoney.IVCurrentMoney {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.bt_trans)
    Button btTrans;
    @BindView(R.id.sv_record)
    SuperTextView svRecord;
    @BindView(R.id.rv_record)
    RecyclerView rvRecord;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private final static int REQUEST_CODE = 0x00001;
    private CurrentMoneyAdapter mAdapter;
    private int page = 1, allDataSize;
    private List<CurrentMoneyBean.ListBean> dataBeans;
    private List<BuyMiniBean.PayWayBean> typeListBeans;
    private CCurrentMoney.IPCurrentMoney mPresenter;
    private String id = "0";    //记录类型ID  默认是0 全部
    private boolean INIT_DATA = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_current_money;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.mine_current_money);
        rvRecord.setLayoutManager(new LinearLayoutManager(mContext));
        btTrans.setVisibility(View.GONE);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            id = "0";
            INIT_DATA = true;
            initData();
        });
        //后退
        llTopLeft.setOnClickListener(v -> {
            setResult(RESULT_OK);
            finish();
        });
        //记录类型选择
        svRecord.setRightTvClickListener(() -> {
            showRecordTypePop();
        });
        //提现
        btTrans.setOnClickListener(v -> startActivityForResult(CashActivity.class,REQUEST_CODE));
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.CURRENT_MONEY_URL, GetParamUtil.getCurrentMoneyParam(page,id));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PCurrentMoney(this);
    }

    //显示记录类型窗口
    private void showRecordTypePop() {
        if (typeListBeans == null) {
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_cash_bi_coin, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_coin);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        PayMethodAdapter areaAdapter = new PayMethodAdapter(R.layout.item_cash_bi_coin, typeListBeans);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            id = typeListBeans.get(position).getId();
            svRecord.setRightString(typeListBeans.get(position).getName());
            page = 1;
            initData();
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showAsDropDown(mContext, svRecord, 0);

    }

    private void initAdapter() {
        mAdapter = new CurrentMoneyAdapter(R.layout.item_current_money, dataBeans);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        rvRecord.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvRecord);
//        mAdapter.setOnItemClickListener((adapter, view, position) -> {
//            mBundle = new Bundle();
//            if (position == 0) {
//                mBundle.putInt(SpMsg.INTENT_CODE, CurrentMoneyDetailActivity.RECHARGE_DETAIL_TYPE);
//            } else if (position == 1) {
//                mBundle.putInt(SpMsg.INTENT_CODE, CurrentMoneyDetailActivity.TRANSFER_DETAIL_TYPE);
//            } else if (position == 2) {
//                mBundle.putInt(SpMsg.INTENT_CODE, CurrentMoneyDetailActivity.HOST_DETAIL_TYPE);
//            } else {
//                mBundle.putInt(SpMsg.INTENT_CODE, CurrentMoneyDetailActivity.EXCHANGE_DETAIL_TYPE);
//            }
//            startActivity(CurrentMoneyDetailActivity.class, mBundle);
//        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK){
            setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK){
            return;
        }
        switch (requestCode){
            case REQUEST_CODE:
                initData();
                break;
        }
    }

    @Override
    public void vGetInitSuccess(CurrentMoneyBean dataBean) {
        dataBeans = dataBean.getList();
        typeListBeans = dataBean.getTypelist();
        allDataSize = dataBean.getTotal();
        tvMoney.setText(dataBean.getBalance());
        //第一次进来时执行的操作
        if (INIT_DATA){
            if (typeListBeans != null && typeListBeans.size() != 0){
                svRecord.setRightString(typeListBeans.get(0).getName());
            }
            INIT_DATA = false;
        }
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(dataBeans);
            } else {
                initAdapter();
            }
        } else {
            if (dataBeans.size() != 0) {
                mAdapter.addData(dataBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }
}
